package cn.ztr.ymcc.mq;

import cn.ztr.ymcc.constance.cons;
import cn.ztr.ymcc.domain.PayOrder;
import cn.ztr.ymcc.dto.PayDto;
import cn.ztr.ymcc.mapper.PayOrderMapper;
import com.alibaba.fastjson.JSON;
import io.netty.util.CharsetUtil;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(
        consumerGroup = "PAY_ORDER_ZTR",
        topic = cons.TX_TOPIC_ZTR ,
        selectorExpression = cons.TX_TARGET_ZTR
)
public class Consumer implements RocketMQListener<MessageExt> {
    @Autowired
    private PayOrderMapper payOrderMapper;

    @Override
    public void onMessage(MessageExt message) {
        //1 获取消息
        byte[] body = message.getBody();
        String s = new String(body, CharsetUtil.UTF_8);
        PayDto payDto = JSON.parseObject(s, PayDto.class);
        //2 创建payorder对象并设置属性
        PayOrder payOrder = new PayOrder();
        BeanUtils.copyProperties(payDto,payOrder);
        //3 添加到数据库
        payOrderMapper.insert(payOrder);
    }
}
